/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package limousine.dbservice;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import limousine.util.Func;
import limousine.vo.Car;

/**
 *
 * @author Tailer
 */
public class CarService {

    private DataSource ds = null;
    private Func func = new Func();

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    public Car getByCarId(String carId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * From CAR where CAR_ID = '" + carId.trim() + "' ");
        Car o = null;
        try {
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql.toString());
            if (rs.next()) {
                o = new Car();
                o.setCarId(rs.getString("CAR_ID"));
                o.setCarName(rs.getString("CAR_NAME"));
                o.setCarNo(rs.getString("CAR_NO"));
                o.setCarType(rs.getInt("CAR_TYPE"));
                o.setStatus(rs.getInt("STATUS"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(rs.getDate("CREATE_DATE"));
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(rs.getDate("UPDATE_DATE"));
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    public boolean insert(Car o) {
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO CAR(CAR_ID,CAR_NAME,CAR_NO,CAR_TYPE,STATUS,CREATE_BY,CREATE_DATE,UPDATE_BY,UPDATE_DATE ) ");
        sql.append("VALUES (?,?,?,?,?,?,?,?,?)");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getCarId());
            stmt.setString(2, o.getCarName());
            stmt.setString(3, o.getCarNo());
            stmt.setInt(4, o.getCarType());
            stmt.setInt(5, o.getStatus());
            stmt.setString(6, o.getCreateBy());
            stmt.setTimestamp(7, func.getTimestamp());
            stmt.setString(8, o.getUpdateBy());
            stmt.setTimestamp(9, func.getTimestamp());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean update(Car o) {
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE CAR ");        
        sql.append("SET CAR_NAME=?,CAR_NO=?,CAR_TYPE=?,STATUS=?,UPDATE_BY=?,UPDATE_DATE=?  ");
        sql.append("where CAR_ID='" + o.getCarId() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getCarName());
            stmt.setString(2, o.getCarNo());
            stmt.setInt(3, o.getCarType());
            stmt.setInt(4, o.getStatus());
            stmt.setString(5, o.getUpdateBy());
            stmt.setTimestamp(6, func.getTimestamp());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteByCarId(String carId) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from CAR where CAR_ID = '" + carId.trim() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteBySQL(String sql) {
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
}
